import { Controller, Get, Post, Body, Patch, Param, Delete,UseInterceptors, UploadedFile , Res} from '@nestjs/common';
import { UploadService } from './upload.service';
import { CreateUploadDto } from './dto/create-upload.dto';
import { UpdateUploadDto } from './dto/update-upload.dto';
import {FileInterceptor,FilesInterceptor } from '@nestjs/platform-express'
import { join } from 'path';
import type { Response } from 'express'
import {zip} from 'compressing'

@Controller('upload')
export class UploadController {
  constructor(private readonly uploadService: UploadService) {}
  // 文件上传需要用到UseInterceptors()中间件来处理文件，如果是单个文件用FileInterceptor ，如果是多个文件用FilesInterceptor
  @UseInterceptors(FileInterceptor('file'))
  @Post('add')
  create(@UploadedFile() file) {
    console.log(file)
    return '上传成功';
    // return this.uploadService.create(createUploadDto);
  }

  @Get('down')
  findAll(@Res() res:Response) {
    // 直接下载文件
    const url = join(__dirname,'../images/1715157210160.png')
    res.download(url)
    // return this.uploadService.findAll();
  }
  @Get('strem')
  async strem(@Res() res:Response) {
    // 以文件流的形式返回
    const url = join(__dirname,'../images/1715157210160.png')
    const tarStream  = new zip.Stream()
    await tarStream.addEntry(url)
    console.log(tarStream)
    res.setHeader('Content-Type', 'application/octet-stream');
   
    res.setHeader(
      'Content-Disposition',  
      `attachment; filename=xiaoman`,
    );
 
    tarStream.pipe(res)
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.uploadService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUploadDto: UpdateUploadDto) {
    return this.uploadService.update(+id, updateUploadDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.uploadService.remove(+id);
  }
}
